package com.csdn.leon.view;

import java.lang.reflect.Field;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.Scroller;
  
public class FFViewPager extends ViewPager {  
  
    private boolean isCanScroll = false;  
  
    public FFViewPager(Context context) {  
        super(context); 
        setDurationTime(context);
    }  
  
    public FFViewPager(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        setDurationTime(context);
    }  
    /**
     * 
     *  方法名称: setDurationTime
     *  功能描述: 设置滑动的持久时间. 
     *  修改记录:
     *  	修改者:   ouyangxingyu198 
     *  	修改日期: 2015年5月31日
     *  	修改内容: 创建.
     *  @param context
     */
    public void setDurationTime(Context context){
    	try {
            Field field = ViewPager.class.getDeclaredField("mScroller");
            field.setAccessible(true);
            ViewPageScroller scroller = new ViewPageScroller(context,
                    new AccelerateInterpolator());
            field.set(this, scroller);
            scroller.setmDuration(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  
    public void setScanScroll(boolean isCanScroll) {  
        this.isCanScroll = isCanScroll;  
    }  
  
    @Override  
    public void scrollTo(int x, int y) {  
        super.scrollTo(x, y);  
    }  
  
    @Override  
    public boolean onTouchEvent(MotionEvent arg0) {  
        // TODO Auto-generated method stub  
        if (isCanScroll) {  
            return super.onTouchEvent(arg0);  
        } else {  
            return false;  
        }  
  
    }  
  
    @Override  
    public void setCurrentItem(int item, boolean smoothScroll) {  
        // TODO Auto-generated method stub  
        super.setCurrentItem(item, smoothScroll);  
    }  
  
    @Override  
    public void setCurrentItem(int item) {  
        // TODO Auto-generated method stub  
        super.setCurrentItem(item);  
    }  
  
    @Override  
    public boolean onInterceptTouchEvent(MotionEvent arg0) {  
        // TODO Auto-generated method stub  
        if (isCanScroll) {  
            return super.onInterceptTouchEvent(arg0);  
        } else {  
            return false;  
        }  
  
    }
    /**
     * 
     *  类名称     : ViewPageScroller
     *  功能描述: 自定义Scroller.
     *  修改记录:
     *  	修改者:   ouyangxingyu198 
     *  	修改日期: 2015年5月31日
     *  	修改内容: 创建.
     *  版本信息: V1.0
     */
    public class ViewPageScroller extends Scroller {
        private int mDuration = 1500;
                                                                                                                
        public ViewPageScroller(Context context) {
            super(context);
        }
                                                                                                                
        public ViewPageScroller(Context context, Interpolator interpolator) {
            super(context, interpolator);
        }
                                                                                                                
        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            // Ignore received duration, use fixed one instead
            super.startScroll(startX, startY, dx, dy, mDuration);
        }
                                                                                                                
        @Override
        public void startScroll(int startX, int startY, int dx, int dy) {
            // Ignore received duration, use fixed one instead
            super.startScroll(startX, startY, dx, dy, mDuration);
        }
                                                                                                                
        public void setmDuration(int time) {
            mDuration = time;
        }
                                                                                                                
        public int getmDuration() {
            return mDuration;
        }
    }

}  